<script setup >
import { ref, inject } from 'vue';
import getRequest from '../utils/lxw_request';
import _ from 'lodash';

const props = defineProps(['isOpenModal', 'info', 'map'])
const emit = defineEmits(['update:isOpenModal'])

function closeModal() {
    emit('update:isOpenModal', false)
}

const edit_info = ref({...props.info})


const getUsers = inject("getUsers")
function confirmEdit() {
    if(_.isEqual(edit_info.value, props.info)) {
        closeModal();
        return;
    }
    const body = props.map.reduce((pre, cur) => {
        edit_info.value[cur['key']] === props.info[cur['key']] ? pre : pre[cur['key']] = edit_info.value[cur['key']];
        return pre
    }, {});
    getRequest(`/admin/users/${edit_info.value['uid']}`, "PUT", new Headers({ 'Content-Type': 'application/json' }), JSON.stringify(body))
        .then(res => res.json())
        .then(json => {
            if (json.code === 200) {
                alert("修改信息成功")
                getUsers();
                closeModal();
            } else {
                alert(json.msg);
            }
        })
        .catch((err) => {
            console.error(err);
        })
}
</script>
<template>
    <div class="edit-user-info-modal"  :class="{open: props.isOpenModal}">
        <div class="content" >
            <div class="header" >
                <img src="/src/assets/images/student/close.svg" alt="Delete" @click="closeModal" />

            </div>
            <div class="main" v-if="props.info" >
                <div class="title" >修改用户信息</div>
                <template v-for="(m, index) in props.map" :key="index" >
                    <div class="input-container" ><div class="item-name" >{{ m['label'] }}</div><input :disabled="index == 0" class="input" :class="{edited: edit_info[m['key']] !== props.info[m['key']]}" type="text" v-model="edit_info[m['key']]"  /></div>
                </template>
            </div>
            <div class="footer" >
                <div class="button confirm" @click="confirmEdit" >确定</div>
                <div class="button cancel " @click="closeModal" >取消</div>
            </div>

        </div>
    </div>

</template>
<style scoped>
.edit-user-info-modal {
    display: none;
    width: 100%;

    position: fixed;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.5);
    overflow: auto;
    z-index: 15;
    width: 630px;
	background-color: #ffffff;
	box-shadow: 0px 3px 6px 0px 
		rgba(0, 0, 0, 0.16);
        top: 50%;
}

.edit-user-info-modal.open {
    display: flex;
    justify-content: center;
    align-items: start;
}


.content {
    width: 100%;
    background-color: #ffffff;
    border: none;
    box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.16);
    padding: 6px 5%;
}


.header {
    display: flex;
    justify-content: end;
    align-items: center;
}

.header>img {
    cursor: pointer;
    user-select: none;
    transition: transform 0.3s;
    scale: 0.7;
}

.header>img:hover {
    transform: rotate(90deg);
}

.main {
    margin-bottom: 20px;
    font-family: Source Han Sans CN;
	font-size: 18px;
	font-weight: normal;
	font-stretch: normal;
	line-height: 20px;
	letter-spacing: 0px;
	color: #707070;
}

.title {
    font-size: 20px;
    text-align: center;
    padding: 10px 0;
    font-family: Source Han Sans CN;
	font-size: 22px;
	font-weight: normal;
    font-weight: 550;
	font-stretch: normal;
	line-height: 20px;
	letter-spacing: 0px;
	color: #707070;
}

.input-container {
    width: 80%;
    margin: 16px auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.item-name {
    letter-spacing: 1px;
    font-size: 18px;
    width: 18%;
}

.item-name::after {
    content: ": "
}




input[type=text] {
    width: 100%;
    height: 35px;
    display: inline-block;
    padding: 5px 10px;
    border: none;
    background-color: #f1f1f1;
    letter-spacing: 1px;
    font-size: 16px;
    line-height: 30px;
    background-color: #f1f2f6;
	box-shadow: 0px 3px 6px 0px 
		rgba(0, 0, 0, 0.16);
	border-radius: 6px;
    font-family: Source Han Sans CN;
	font-weight: normal;
	font-stretch: normal;
	line-height: 20px;
	letter-spacing: 0px;
	color: #707070;

}

input[type=text]:focus {
    outline: none;
    background-color: #C0C4CC;
}

input[type=text].edited {
    background-color:  #fcd3d3;
}

input[type=text].edited:focus {
    outline: none;
    background-color:  #f89898;
}


.footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 18%;
    padding-right: 18%;

}   

.button {
    display: inline-block;
    width: 70px;
    height: 30px;
    border-radius: 5px;
    color: #ffffff;
    line-height: 30px;
    letter-spacing: 1px;
    text-align: center;
    cursor: pointer;
    user-select: none;
    opacity: 1;
    margin: 0 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 130px;
	height: 35px;
    margin-bottom: 3%;
}

.button:hover {
    opacity: 0.8;
}

.button:active {
    transform: translateY(3px);
}

.button.confirm {
    background-color: #8394a6;
}

.button.cancel {
    background-color: #939393;
}

</style>